<?php
namespace app\wx\controller;
use think\Controller;
use Util\Sysdb;
use Util\Tool;


class Basewx extends Controller
{
    public function __construct(){
        parent::__construct();
//        header("Access-Control-Allow-Origin:*");
//        header('Access-Control-Allow-Methods:POST');
//        header('Access-Control-Allow-Headers:x-requested-with, content-type');
        $this->Tool= new Tool;
        $this->myurl='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
        $this->model = model('Weixin');
        $this->db = new Sysdb();
        $txt = PHP_EOL.date("Y-m-d H:i:s",time()).PHP_EOL;
        $txt .= PHP_EOL.file_get_contents("php://input").PHP_EOL;
        $txt .= PHP_EOL.json_encode($_GET,JSON_UNESCAPED_UNICODE).PHP_EOL;
        $txt .= PHP_EOL.json_encode($_POST,JSON_UNESCAPED_UNICODE).PHP_EOL;
        $txt .= PHP_EOL.json_encode($_REQUEST,JSON_UNESCAPED_UNICODE).PHP_EOL;
        $txt .= PHP_EOL.json_encode($_FILES,JSON_UNESCAPED_UNICODE).PHP_EOL;
        $txt .= PHP_EOL.json_encode(session('wxgzh'),JSON_UNESCAPED_UNICODE).PHP_EOL;
        $this->Tool->writeLog($txt,'wx_channel_data');
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') == false) {
            exit('请使用微信访问');
        }
        //session(null);
        //dump(session('wxgzh'));
        $user = $this->db->table('wxgzh')->where(['phone'=>'17702752517'])->item();
        if(!$user){
            session(null);
        }
        if(input('get.code')){
            //通过auth()方法获取code
            $code = input('get.code');
            $this->Tool->writeLog(date('Y-m-d-H-i-s',time()).PHP_EOL.$code,'wx_user_code');
            //使用auth_access_token($code,false)方法得到网页授权access_token
            $res = $this->model->auth_access_token($code);
            $auth_access_token = $res['access_token'];
            $openid = $res['openid'];
            //拉取用户信息(需scope为 snsapi_userinfo)
            $userinfo = $this->model->get_userinfo($auth_access_token,$openid);
            //dump($userinfo);die;
            if(empty($userinfo['openid'])){
                //header('Location:'.$this->myurl);
                dump($userinfo);echo '联系客服';die;
            }
            $user = $this->db->table('wxgzh')->where(['openid'=>$userinfo['openid']])->item();
            $data = $userinfo;
            $data['privilege'] = json_encode($userinfo['privilege'],JSON_UNESCAPED_UNICODE);
            $data['up_time'] = time();
            if($user){
                $this->db->table('wxgzh')->where(['openid'=>$userinfo['openid']])->update($data);
                $ua = $this->db->table('user_account')->where(['ua_phone'=>$user['phone']])->item();
                $user['ua_id'] = $ua['ua_id'];
                session('wxgzh',$user);
            }else{
                $data['add_time'] = time();
                $this->db->table('wxgzh')->insert($data);
                $data = $data+$user;
                session('wxgzh',$data);
            }
        }
        //dump(urlEncode($this->myurl));
        //dump(session('wxgzh'));
        if(empty(session('wxgzh'))){
            //回调地址，执行userinfo方法
            //$redirect = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
            //echo($this->myurl);die;
            //按钮跳转地址
            $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('wx.appid').'&redirect_uri='.urlEncode($this->myurl).'&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
            header('Location:'.$url);
        }
        if(empty(session('wxgzh')['phone'])){
            $see = $this->db->table('wxgzh')->where(['openid'=>session('wxgzh')['openid']])->leftJoin('wxgzh',['ty_wxgzh.ua_id=ty_user_account.ua_id'])->item();
            //dump($see);die;
            if(empty($see['phone'])){
                $this->assign('url',$this->myurl);
                echo $this->fetch('wx@wx/tel');die;
            }else{
                session('wxgzh',$see);
            }
        }
        // $data = [
        // "id" => 5,
        // "ua_id" => 28,
        // "openid" => "oPcHts09eGE37JdN2eEo9Dy76kL0",
        // "nickname" => "汪焕波",
        // "phone" => "17702752517",
        // "sex" => 1,
        // "language" => "zh_CN",
        // "city" => "武汉",
        // "country" => "中国",
        // "province" => "湖北",
        // "headimgurl" => "http://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIllAfIgf3AH28iaafg8W5hERGbCZZypAwxBdELQ2PMIejYahyGg98LjWoHMpKicWoPsobw1YfFWGXg/132",
        // "privilege" => "",
        // "headimgurl_file" => "/public/gzh_logo/20191030171007.jpg",
        // "add_time" => '1571882012',
        // "up_time" => '1573611975',
        // ];
        // session('wxgzh',$data);
        //dump( session('wxgzh'));
    }

}